import { Dispatch } from 'redux';
import type { IListData, IStateType } from './index.d';

export const MODULE_NAME: string = 'CampusSecurity/monitor';

export const types = {
  DATA_SOURCE: `${MODULE_NAME}/DATA_SOURCE`, // 列表数据
}

export default (state: IStateType = {
  dataSource: []
}, action: any) => {
  switch (action.type) {
    case types.DATA_SOURCE:
      return Object.assign({}, state, {
        dataSource: action.data
      })
    default: return state
  }
}

/**
 * 获取列表数据
*/
export const getList = (keyword: string = '', tableKey: string = 'all') => {
  return async (dispatch: Dispatch): Promise<void> => {
    try {
      const data: IListData = [{
        title: '家乐园小学教学楼楼道口1',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '1',
        id: '3bdff3e3d95d4d84842df6c505d02a04'
      }, {
        title: '家乐园小学教学楼楼道口2',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '1',
        id: 'bf674ab715f040fdbefe30e94defdd01'
      }, {
        title: '家乐园小学教学楼楼道口3',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '1',
        id: 'da54a857d22a42bdac9b198a07b5fcae'
      }, {
        title: '家乐园小学教学楼楼道口4',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '1',
        id: '12830695f3f24ed6bff56b8f8c3a13d1'
      }, {
        title: '龙泉小学一楼北门',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '2',
        id: '3e5c8790efa2473ab9bb05e86d18930a'
      }, {
        title: '龙泉小学一楼南门',
        image: require('@/assets/images/monitor_cover.jpg'),
        key: '2',
        id: 'a98909c98a5148cf9d00a9e6ff3ca182'
      }]

      dispatch({
        type: types.DATA_SOURCE,
        data: data.filter(item => item.title.includes(keyword) && (item.key === tableKey || tableKey === 'all'))
      })
    } catch { }
  }
}
